package filters;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import data.UserData;

import systems.ConfigSystem;

import models.UserModel;
import models.config.SiteConfigModel;

public final class UrlFilter implements Filter {
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		SiteConfigModel config = ConfigSystem.getConfigs();
		
		UserModel user = null;
		if (req.getUserPrincipal() != null) {
			String username = req.getUserPrincipal().getName();
			UserData uData = new UserData();
			user = uData.getUser(username);
		}
		
		request.setAttribute("user", user);
		request.setAttribute("config", config);
		chain.doFilter(request, response);
	}

	public void destroy() {

	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
	}
}